package com.jxstjh.conch.support.utils;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

/**
 * 账号信息工具类
 * 短信、邮件发送渠道会涉及到发送账号
 * 账号要可配置, 并由此工具类来获取账号信息
 */
@Component
public class AccountUtil {

    @Autowired
    private StringRedisTemplate redisTemplate;

    //TODO 如果这两类发送渠道由信息中心来实现, 需要将多个发送账号信息可配置, 配置在Redis中, key为SEND:ACCOUNT:SMS:1
    public <T> T getAccount(String sendChannel, Integer sendAccount, T t) {

        String key = "SEND:ACCOUNT:" + sendChannel.toUpperCase() + ":" + sendAccount;
        String account = redisTemplate.opsForValue().get(key);
        JSONObject jsonObject = JSONObject.parseObject(account);
        if (jsonObject != null) {
            return (T) jsonObject;
        }
        return null;
    }
}